<?php

class FileSystemLibrary extends FileManagerLibrary {
	
	private $root;
	private $displayFilesInTree;
	
	private $isReadOnly = false;
	
	//private $lastErrorText;
	/**
	 * 
	 */
	public function serialize() {

		$data['root'] = $this->root;
		return serialize($data);		
	}

	/**
	 * @param $serialized
	 */
	public function unserialize($serialized) {
		
		$data = unserialize($serialized);
		$this->root = $data['root'];
		
	}
	
	public function __construct($name, $path) {
		parent::setName($name);
		$this->root = $path;
		$this->displayFilesInTree = false;		
	}
	
	public function setReadOnly($v=true) {
		$this->isReadOnly = $v;
	}
	
	/*
	public function getLastError() {
		return $this->lastErrorText;
	}
	*/
	public function getSubTree($dir) {
		
		$tree = array();
		
		if( file_exists($this->root . $dir) ) {
			$files = @scandir($this->root . $dir);
			@natcasesort($files);
			if( count($files) > 2 ) { /* The 2 accounts for . and .. */

				// All dirs first
				foreach( $files as $file ) {
					if( file_exists($this->root . $dir . $file) && $file != '.' && $file != '..' && is_dir($this->root . $dir . $file) ) {
						array_push($tree, array('name'=>$file, 'type' => 'directory'));
					}
				}
				
				if($this->displayFilesInTree) {
					// All files
					foreach( $files as $file ) {
						if( file_exists($this->root . $dir . $file) && $file != '.' && $file != '..' && !is_dir($this->root . $dir . $file) ) {
							$ext = preg_replace('/^.*\./', '', $file);
							array_push($tree, array('name'=>$file, 'type' => 'file', 'extension' => $ext));
						}
					}
				}
				
			}
		}
		else {
			echo "Dir '$this->root"."$dir' not found";
		}
		
		return $tree;
	}
	
	public function getFolder($path) {
		
		$ret = array();

		
		if( file_exists($this->root . $path) ) {
			$files = @scandir($this->root . $path);
			@natcasesort($files);
			if( count($files) > 2 ) { /* The 2 accounts for . and .. */
	
				// All dirs first
				foreach( $files as $file ) {
					
					if($file != "." && $file != "..") {
					
						$fileInfo = $this->getInfo($path . $file);
						$ret[utf8_encode($file)] = $fileInfo;
					}
							
				}
			}
		}
		else {
			echo "folder " . $this->root . $path ." not exists";
		}
		return $ret;
		
	}
	
	public function getInfo($path) {
		
		$fullpath = $this->root . $path;
		
		$pathinfo = pathinfo($fullpath);
		$filename = $pathinfo['basename'];
		
		$filetype = is_dir($fullpath)?"dir":$pathinfo['extension'];
		$filetypetitle = $filetype=="dir"?"Directory":($filetype!=""?$filetype."-File":"Unknown"); 
		$preview = $this->getIconUrl($filetype);
		
				
		$changeTime = filemtime($fullpath);
		$createTime = filectime($fullpath);
		$filesize = ($filetype=="dir"?null:filesize($fullpath));
		
		
		$info = array('Path'=> utf8_encode($path),
					  'Filename'=>utf8_encode($filename),
					  'File Type'=>$filetype,
					  'File Type Title' => $filetypetitle,
					  'Preview'=>$preview,
					  'Properties'=>array(
						  'Date Created'=>$createTime!=null?date('d M Y H:i', $createTime):null, 
						  'Date Modified'=>$changeTime!=null?date('d M Y H:i', $changeTime):null, 
						  'Height'=>null,
						  'Width'=>null,
						  'Size'=>$filesize
					  ),
					  'Error'=>"",
					  'Code'=>0
		);
		
		return $info;
	}
	
	public function addFolder($path, $name) {
		
		if($this->isReadOnly) return;
		
		$realdir = $this->root . $path . $name;
		
		if(!file_exists($realdir)) {
			$old_umask = umask(0);
			if(@mkdir($realdir,0777)) {
				 // the default chmod
				 //$e = error_get_last();
				 //$this->lastErrorText = $e['message'];
				umask($old_umask);
				return $name;
			}
			else {
				umask($old_umask);
				return null;
			}
		}
	}
	
	public function addFile($path, $file) {
		
		if($this->isReadOnly) return;
		
		$filename = $file['name'];
		$fullpath = $this->root . $path . $filename;
		
		if(!file_exists($fullpath)) {
			
			if(@move_uploaded_file($file['tmp_name'], $fullpath)) {
				return $filename;
			}
			
		}
		
		return $filename;	
	}
	
	public function downloadFile($path) {
		
		$fullpath = $this->root . $path;
		
		if(file_exists($fullpath)) {
			
			$name = basename($path);
			$this->sendFile($fullpath, $name);
			
		}
		else {
			die("File not found");
		}
		
	}
/**
	 * @param $path
	 */
	public function rename($path, $new) {
		
		if($this->isReadOnly) return;
		
		$fullpath = $this->root . $path;
		
		// Get New Full Path
		$info = pathinfo($path);
		$fullpath_new = $this->root . $info['dirname'] . "/" . $new; 
		
		//Check if new File/Folder not exists
		if(file_exists($fullpath_new)) {
			return;
		}
		
		if(file_exists($fullpath)) {
			if(rename($fullpath, $fullpath_new)) {
				return $new;
			}			
			else {
				return;
			}
		}
		
	}

/**
	 * @param $path
	 */
	public function delete($path) {
		
		if($this->isReadOnly) return;
		
		$fullpath = $this->root . $path;
		
		//Check if new File/Folder not exists
		if(!file_exists($fullpath)) {
			return;
		}
		else {
			$this->deleteRecursive($fullpath);
			return true;
		}
		
	}
	
	private final function deleteRecursive($path) {
		
		//echo "scanning $path\n";
		if(is_dir($path)) {
			
			// First Delete all Files and Folders in CUrrent Path
			$files = @scandir($path);

			if(count($files) > 2 ) { /* The 2 accounts for . and .. */
				// All dirs first
				foreach( $files as $file ) {
					if($file != "." && $file != "..") {
						$this->deleteRecursive($path . "/" . $file);
					}
				}
			}
		
			@closedir(@opendir($path));
			rmdir($path);
		}
		else {
			@unlink($path);
		}
		
	}


}

?>